package com.leetcode.problems058;

/**
 * https://leetcode-cn.com/problems/length-of-last-word/comments/
 * @Date 2021/12/09 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {

    public int lengthOfLastWord(String s) {
        int startIndex = s.length() - 1;
        while ( startIndex >= 0 && s.charAt(startIndex) == ' '){
            startIndex--;
        }
        if(startIndex < 0){
            return 0;
        }
        int endIndex = startIndex;
        while (endIndex >= 0 && s.charAt(endIndex) != ' '){
            endIndex--;
        }

        return startIndex - endIndex;
    }



    public static void main(String[] args) {
        String str = "Hello World ";
        System.out.println(new Solution().lengthOfLastWord(str));
    }
}
